﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LinQ_DTO;

namespace DAO
{
    public class UsersInRoleDAO
    {
        public List<aspnet_UsersInRole> LayDanhSach()
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            var list = from c in EFashionDB.aspnet_UsersInRoles 
                       select c;
            return list.ToList();
        }

        public aspnet_UsersInRole LayTheoUserId(Guid userId)
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            var list = from c in EFashionDB.aspnet_UsersInRoles
                       where c.UserId == userId
                       select c;
            return list.Single();

        }

        public aspnet_UsersInRole LayTheoRoleId(Guid roleId)
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            var list = from c in EFashionDB.aspnet_UsersInRoles
                       where c.RoleId == roleId
                       select c;
            return list.Single();

        }

        public void Them(aspnet_UsersInRole temp)
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            EFashionDB.aspnet_UsersInRoles.InsertOnSubmit(temp);
            EFashionDB.SubmitChanges();

        }

        public void Xoa(aspnet_UsersInRole temp)
        {

            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            var list = from c in EFashionDB.aspnet_UsersInRoles
                          where c.RoleId == temp.RoleId && c.UserId == temp.UserId
                          select c;
            aspnet_UsersInRole Result = list.Single();
            EFashionDB.aspnet_UsersInRoles.DeleteOnSubmit(Result);
            EFashionDB.SubmitChanges();


        }

        public void CapNhatRoleId(aspnet_UsersInRole temp)
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            var list = from c in EFashionDB.aspnet_UsersInRoles
                       where c.UserId == temp.UserId
                          select c;

            foreach (aspnet_UsersInRole u in EFashionDB.aspnet_UsersInRoles)
            {

                u.RoleId = temp.RoleId;
            }

            EFashionDB.SubmitChanges();

        }

        public void CapNhatUserId(aspnet_UsersInRole temp)
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            var list = from c in EFashionDB.aspnet_UsersInRoles
                       where c.RoleId == temp.RoleId
                       select c;

            foreach (aspnet_UsersInRole u in EFashionDB.aspnet_UsersInRoles)
            {

                u.UserId = temp.UserId;
            }

            EFashionDB.SubmitChanges();

        }

        /************************************************************************************************************        *                          coder: TRAN KINH LY 
         ***********************************************************************************************************/
        public List<aspnet_UsersInRole> LayDanhSachTheoRoleID(Guid roleID)
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            var list = from c in EFashionDB.aspnet_UsersInRoles
                       where c.RoleId == roleID
                       select c;

            return list.ToList();
        }

        /************************************************************************************************************        *                          coder: TRAN KINH LY 
         ***********************************************************************************************************/




    }
}
